﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HL.Lib.Global;
using HL.Lib.MVC;
using HL.Lib.Models;
namespace HL.Lib.CPControllers
{
    class MMapCityController : CPController
    {
        private MMapCityEntity _entity = null;
        public MMapCityController()
        {
            //khoi tao Service
            DataService = MMapCityService.Instance;
            CheckPermissions = true;
        }
        public void ActionIndex(MMapCityModel model)
        {
            // sap xep tu dong
            string orderBy = AutoSort(model.Sort);

            // tao danh sach

            MMapCityService service = new MMapCityService();
            /* List<MMapLocationEntity> dbQuery = service.GetList()
                                //.OrderBy(orderBy)
                                  .Skip(model.PageIndex * model.PageSize).Take(model.PageSize).ToList();
             */
            List<MMapCityEntity> dbQuery = MMapCityService.Instance.CreateQuery().Where(m => m.DeleteFlag == 0)
                .Take(model.PageSize)
                                .OrderBy(orderBy)
                                .Skip(model.PageIndex * model.PageSize).ToList();

           // dbQuery = service.GetCityList();
            if (dbQuery != null && dbQuery.Count > 0)
            {
                ViewBag.Data = dbQuery.ToList();
                model.TotalRecord = service.GetCityList().Count;
                ViewBag.Model = model;
            }
            else
            {
                ViewBag.Data = new MMapLocationEntity();
                model.TotalRecord = 0;
                ViewBag.Model = model;
            }
        }
        public void ActionAdd(MMapCityModel model)
        {
            if (model.RecordID > 0)
            {
                _entity = MMapCityService.Instance.GetByID(model.RecordID);
                
                // khoi tao gia tri mac dinh khi update
            }
            else
            {
                _entity = new MMapCityEntity();
                //_entity.MapId = MMapLocationService.Instance.GetMaxId();
              
                // khoi tao gia tri mac dinh khi insert
                // entity.Order = GetMaxOrder(model);
            }

            ViewBag.Data = _entity;
            ViewBag.Model = model;
        }

        public void ActionSave(MMapCityModel model)
        {
            if (ValidSave(model))
                SaveRedirect();
        }

        public void ActionApply(MMapCityModel model)
        {
            if (ValidSave(model))
                ApplyRedirect(model.RecordID, _entity.ID);
        }

        public void ActionSaveNew(MMapCityModel model)
        {
            if (ValidSave(model))
                SaveNewRedirect(model.RecordID, _entity.ID);
        }


        private bool ValidSave(MMapCityModel model)
        {
            TryUpdateModel(_entity);

            ViewBag.Data = _entity;
            ViewBag.Model = model;
            CPViewPage.Message.MessageType = Message.MessageTypeEnum.Error;
            //kiem tra ten 
            if (_entity.CityName.Trim() == string.Empty)
            {
                CPViewPage.Message.ListMessage.Add("Nhập tên tỉnh/thành phố.");
            }
            if (_entity.X.Trim() == string.Empty || _entity.Y.Trim() == string.Empty)
            {
                CPViewPage.Message.ListMessage.Add("Nhập kinh độ, vĩ độ.");
            }

            if (CPViewPage.Message.ListMessage.Count == 0)
            {
                try
                {
                    int a = MMapCityService.Instance.Save(_entity);
                    //save
                    /*
                    if (_entity.ID == null)
                    {
                        int a = MMapLocationService.Instance.Save(_entity);
                    }
                    else
                    {
                        MMapLocationService.Instance.Update("[ID]=" + _entity.ID, "@Title", "aaaa");
                        //int a = MMapLocationService.Instance.Update(o=>o.ID==_entity.ID,"@Value",_entity);
                         //MMapLocationService.Instance
                    }*/
                }
                catch (Exception ex)
                {
                    Global.Error.Write(ex);
                    CPViewPage.Message.ListMessage.Add(ex.Message);
                    return false;
                }
                return true;
            }
            return false;
        }

    }
}
